package pos.datos;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import pos.dominio.*;

public class JDBCHorarioDAO implements IHorarioDAO{

	@Override
	public List obtenerHorario(String zona) {
		
		Connection conn = ConnectionManager.getInstance().checkOut();
        PreparedStatement stmt = null;
        List searchResults = new LinkedList();
        ResultSet result = null;
        
        try {

            String sql = "SELECT * FROM horario WHERE ( IDZona = ? )";
            stmt = conn.prepareStatement(sql); 
            stmt.setString(1, zona);
            stmt.executeQuery();
            result = stmt.executeQuery();
            

            while (result.next()) {
            
                    Horario temp = new Horario();
                    
                    temp.setIDHorario(result.getString("IDHorario"));
                    temp.setHoraInicio(result.getTime("HorarioInicio"));
                    temp.setHoraFin(result.getTime("HorarioFin"));
                    temp.setPlazas_disponible(result.getInt("PlazasDisponibles"));
                    temp.setIDZona(result.getString("IDZona"));
                    
                    searchResults.add(temp);
            }
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            ConnectionManager.getInstance().checkIn(conn);
            try {
                if (result != null)
                    result.close();
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }

        }
        return searchResults;
	}

	@Override
	public Horario obtenerHorarioID(String id) {
		
	
		Connection conn = ConnectionManager.getInstance().checkOut();
        PreparedStatement stmt = null;
        Horario temp = new Horario();
        ResultSet result = null;
        
        try {

            String sql = "SELECT * FROM horario WHERE ( IDHorario = ? )";
            stmt = conn.prepareStatement(sql); 
            stmt.setString(1, id);
            stmt.executeQuery();
            result = stmt.executeQuery();
            result.next();
            temp.setIDHorario(result.getString("IDHorario"));
            temp.setHoraInicio(result.getTime("HorarioInicio"));
            temp.setHoraFin(result.getTime("HorarioFin"));
            temp.setPlazas_disponible(result.getInt("PlazasDisponibles"));
            temp.setIDZona(result.getString("IDZona"));
                    
                   
           
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            ConnectionManager.getInstance().checkIn(conn);
            try {
                if (result != null)
                    result.close();
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }

        }
        return temp;
	}
	
	public void actualizarPlazas(int nuevaCantidad,String Idh){
		Connection conn = ConnectionManager.getInstance().checkOut();
		PreparedStatement stmt = null;
		String sql = "UPDATE horario SET PlazasDisponibles = ? WHERE ( IDHorario = ? )";
    try {
        
        stmt = conn.prepareStatement(sql);
        stmt.setInt(1,nuevaCantidad);
        stmt.setString(2, Idh);
        stmt.executeUpdate();

      
    } catch (SQLException e) {
        System.out.println("Message: " + e.getMessage());
        System.out.println("SQLState: " + e.getSQLState());
        System.out.println("ErrorCode: " + e.getErrorCode());
    } finally {
        ConnectionManager.getInstance().checkIn(conn);
        try {
            
            if (stmt != null)
                stmt.close();
        } catch (SQLException e) {
        }
    }
}
	
}
	
	
	
